<?php

/*  File: OutputXML.inc.php

  Notes:
  1st version is for products.xml used by Cost Biter, this however needs to be more generic

  Change History:
  03/10/2009 [1.00] Initial Version
  05/10/2009 [1.01] Construct Created to initialise the DOM to whatever xml file
  06/10/2009 [1.02] products.xml has now changed!! 2 two levels added, need Top Level, Master and Sub Categories
  07/10/2009 [1.03] Added GetTopProducts [beta] for Cost Biter Homepage
  16/10/2009 [1.04] GetSingleProduct function added
  22/10/2009 [1.05] AllProductInfo function for individual stockcode xml files
 */

class OutputXML {

    private $DOMFile;
    private $TopLevel;
    private $Master;
    private $Sub;
    private $ProductItem;
    private $TopLevelTag;

    public function __construct($file) {
        $DOM = new DOMDocument();
        //$DOM = new DOMDocument('1.0', 'utf-8');
        $DOM->preserveWhiteSpace = false;
        $DOM->load($file);
        $DOM->formatOutput = true;
        $this->FileName = $file;
        $this->TopLevel = "Category";
        $this->Master = "Master";
        $this->Sub = "Sub";
        $this->ProductItem = "Productitem";
        $this->DOMFile = $DOM;
        $this->TopLevelTag = $DOM->getElementsByTagName($this->TopLevel);
    }

    public function GetStockCount($tpl, $masl='', $subl='', $filters='') {
        if ($filters[3] != '') {
            $PMin = $filters[0];
            $PMax = $filters[3];
        } else {
            $Bnd = $filters[0];
        }
        for ($i = 0; $i < $this->TopLevelTag->length; $i++) {
            if (strtolower($this->TopLevelTag->item($i)->getAttribute("Name")) == strtolower($tpl)) {
                $newitems = $this->TopLevelTag->item($i)->getElementsByTagName($this->Master);
                $count = $this->TopLevelTag->item($i)->getElementsByTagName($this->ProductItem);
                $counter = $count->length;
                $i = $this->TopLevelTag->length;
            }
        }
        if ($masl != '') {
            for ($y = 0; $y < $newitems->length; $y++) {
                if (strtolower($newitems->item($y)->getAttribute("Name")) == strtolower($masl)) {
                    $newitem = $newitems->item($y)->getElementsByTagName($this->Sub);
                    $count = $newitems->item($y)->getElementsByTagName($this->ProductItem);
                    $counter = $count->length;
                    $y = $newitems->length;
                }
            }
        }
        if ($subl != '') {
            for ($y = 0; $y < $newitem->length; $y++) {
                if (strtolower($newitem->item($y)->getAttribute("Name")) == strtolower($subl)) {
                    $newsitem = $newitem->item($y)->getElementsByTagName($this->ProductItem);
                    $count = $newitem->item($y)->getElementsByTagName($this->ProductItem);
                    $counter = $count->length;
                    $y = $newitem->length;
                }
            }
        }
        if ($filters[0] != '') {
            $counter = 0;
            for ($a = 0; $a < $count->length; $a++) {
                if ($Bnd != '') {
                    if (strtolower($count->item($a)->getElementsByTagName("Brand")->item(0)->nodeValue) === $Bnd) {
                        $counter++;
                    }
                } else {
                    if ($count->item($a)->getElementsByTagName("Ourprice")->item(0)->nodeValue >= $PMin and $count->item($a)->getElementsByTagName("Ourprice")->item(0)->nodeValue < $PMax) {
                        $counter++;
                    }
                }
            }
        }
        return $counter;
    }

    public function GetTopLevelCategories($Limit=0) {
        if ($Limit == 0 or $Limit > $this->TopLevelTag->length) {
            $Limit = $this->TopLevelTag->length;
        }
        for ($i = 0; $i < $Limit; $i++) {
            $Categories[] = $this->TopLevelTag->item($i)->getAttribute("Name");
        }
        return $Categories;
    }

    public function GetMasterCategories($Category, $Limit=0) {
        for ($i = 0; $i < $this->TopLevelTag->length; $i++) {
            if (strtolower($this->TopLevelTag->item($i)->getAttribute("Name")) == strtolower($Category)) {
                $newitems = $this->TopLevelTag->item($i)->getElementsByTagName($this->Master);
                $newitems_length = $newitems->length;
                $i = $this->TopLevelTag->length;
            }
        }
        if ($Limit == 0 or $Limit > $newitems_length) {
            $Limit = $newitems_length;
        }
        for ($i = 0; $i < $Limit; $i++) {
            $Categories[] = $newitems->item($i)->getAttribute("Name");
        }
        return $Categories;
    }

    public function GetSubCategories($Category, $Master, $Limit=0) {
        for ($i = 0; $i < $this->TopLevelTag->length; $i++) {
            if (strtolower($this->TopLevelTag->item($i)->getAttribute("Name")) == strtolower($Category)) {
                $newitems = $this->TopLevelTag->item($i)->getElementsByTagName($this->Master);
                $newitems_length = $newitems->length;
                $i = $this->TopLevelTag->length;
                for ($y = 0; $y < $newitems->length; $y++) {
                    if (strtolower($newitems->item($y)->getAttribute("Name")) == strtolower($Master)) {
                        $newitem = $newitems->item($y)->getElementsByTagName($this->Sub);
                        $newitem_length = $newitem->length;
                        $y = $newitems->length;
                    }
                }
            }
        }

        if ($Limit == 0 or $Limit > $newitem_length) {
            $Limit = $newitem_length;
        }
        for ($i = 0; $i < $Limit; $i++) {
            $Categories[] = $newitem->item($i)->getAttribute("Name");
        }
        return $Categories;
    }

    public function GetProducts($Category, $Master, $Sub, $Limit=0) {
        for ($x = 0; $x < $this->TopLevelTag->length; $x++) {
            if (strtolower($this->TopLevelTag->item($x)->getAttribute("Name")) == strtolower($Category)) {
                $proditems = $this->TopLevelTag->item($x)->getElementsByTagName($this->Master);
                $proditems_length = $proditems->length;
                $x = $this->TopLevelTag->length;
                for ($y = 0; $y < $proditems->length; $y++) {
                    if (strtolower($proditems->item($y)->getAttribute("Name")) == strtolower($Master)) {
                        $proditem = $proditems->item($y)->getElementsByTagName($this->Sub);
                        $proditem_length = $proditem->length;
                        $y = $proditems->length;
                        for ($z = 0; $z < $proditem->length; $z++) {
                            if (strtolower($proditem->item($z)->getAttribute("Name")) == strtolower($Sub)) {
                                $prod = $proditem->item($z)->getElementsByTagName($this->ProductItem);
                                $prod_length = $prod->length;
                                $z = $proditem->length;
                            }
                        }
                    }
                }
            }
        }
        if ($Limit == 0 or $Limit > $prod_length) {
            $Limit = $prod_length;
        }
        for ($i = 0; $i < $Limit; $i++) {
            $Products[] = array(
                'Name' => $prod->item($i)->getAttribute("Name"),
                'Brand' => $prod->item($i)->getElementsByTagName("Brand")->item(0)->nodeValue,
                'StockCode' => $prod->item($i)->getElementsByTagName("Stockcode")->item(0)->nodeValue,
                'Description' => $prod->item($i)->getElementsByTagName("Description")->item(0)->nodeValue,
                'RRP' => $prod->item($i)->getElementsByTagName("Rrp")->item(0)->nodeValue,
                'OurPrice' => $prod->item($i)->getElementsByTagName("Ourprice")->item(0)->nodeValue,
                'Saving' => $prod->item($i)->getElementsByTagName("Saving")->item(0)->nodeValue,
                'URL' => $prod->item($i)->getElementsByTagName("Link")->item(0)->nodeValue,
                'IMG' => $prod->item($i)->getElementsByTagName("Image")->item(0)->nodeValue
            );
        }
        return $Products;
    }

    public function GetTopProducts($Category, $Limit=0) {
        $Products = Array();
        $MCats = $this->GetMasterCategories($Category);
        foreach ($MCats as $mc) {
            $SCats = $this->GetSubCategories($Category, $mc);
            foreach ($SCats as $sc) {
                $new = $this->GetProducts($Category, $mc, $sc, 3);
                if (count($Products) != 0) {
                    $Products = array_merge($Products, $new);
                } else {
                    $Products = $new;
                }
                if (count($Products) >= 3) {
                    break 2;
                }
            }
        }
        $Products = array_slice($Products, 0, $Limit);
        return $Products;
    }

    public function GetSingleProduct($StockCode) {
        for ($x = 0; $x < $this->TopLevelTag->length; $x++) {
            $proditems = $this->TopLevelTag->item($x)->getElementsByTagName($this->Master);
            $proditems_length = $proditems->length;
            for ($y = 0; $y < $proditems->length; $y++) {
                $proditem = $proditems->item($y)->getElementsByTagName($this->Sub);
                $proditem_length = $proditem->length;
                for ($z = 0; $z < $proditem->length; $z++) {
                    $prod = $proditem->item($z)->getElementsByTagName($this->ProductItem);
                    $prod_length = $prod->length;
                    for ($i = 0; $i < $prod_length; $i++) {
                        if ($prod->item($i)->getElementsByTagName("Stockcode")->item(0)->nodeValue === $StockCode) {
                            $itemList[] = array(
                                'Name' => $prod->item($i)->getAttribute("Name"),
                                'Brand' => $prod->item($i)->getElementsByTagName("Brand")->item(0)->nodeValue,
                                'StockCode' => $prod->item($i)->getElementsByTagName("Stockcode")->item(0)->nodeValue,
                                //					'tpl'			=>$prod->item($i)->getElementsByTagName("tpl")->item(0)->nodevalue,
                                //					'mcl'			=>$prod->item($i)->getElementsByTagName("mcl")->item(0)->nodevalue,
                                //					'scl'			=>$prod->item($i)->getElementsByTagName("scl")->item(0)->nodevalue,
                                'Description' => $prod->item($i)->getElementsByTagName("Description")->item(0)->nodeValue,
                                'RRP' => $prod->item($i)->getElementsByTagName("Rrp")->item(0)->nodeValue,
                                'OurPrice' => $prod->item($i)->getElementsByTagName("Ourprice")->item(0)->nodeValue,
                                'Saving' => $prod->item($i)->getElementsByTagName("Saving")->item(0)->nodeValue,
                                'URL' => $prod->item($i)->getElementsByTagName("Link")->item(0)->nodeValue,
                                'IMG' => $prod->item($i)->getElementsByTagName("Image")->item(0)->nodeValue
                            );
                            $x = $this->TopLevelTag->length;
                            $y = $proditems->length;
                            $z = $proditem->length;
                            $i = $prod->length;
                        }
                    }
                }
            }
        }
        return $itemList;
    }

    public function AllProductInfo() {
        $Product = array(
            'Name' => $this->DOMFile->getElementsByTagName("Name")->item(0)->nodeValue,
            'StockCode' => $this->DOMFile->getElementsByTagName("Stockcode")->item(0)->nodeValue,
            'tpl' => $this->DOMFile->getElementsByTagName("tpl")->item(0)->nodeValue,
            'mcl' => $this->DOMFile->getElementsByTagName("mcl")->item(0)->nodeValue,
            'scl' => $this->DOMFile->getElementsByTagName("scl")->item(0)->nodeValue,
            'ShortDescription' => $this->DOMFile->getElementsByTagName("ShortDescription")->item(0)->nodeValue,
            'RRP' => $this->DOMFile->getElementsByTagName("RRP")->item(0)->nodeValue,
            'OurPrice' => $this->DOMFile->getElementsByTagName("OurPrice")->item(0)->nodeValue,
            'Saving' => $this->DOMFile->getElementsByTagName("Saving")->item(0)->nodeValue,
            'Features' => $this->DOMFile->getElementsByTagName("Features")->item(0)->nodeValue,
            'Specifications' => $this->DOMFile->getElementsByTagName("Specifications")->item(0)->nodeValue
        );
        return $Product;
    }

}
?>